var app = getApp();
const useful_function = require('../../utils/usefulfunction.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
      list: [],
    },

    //触发折叠展开
    activeFold(event) {
        var id = event.currentTarget.dataset.id
        var isfold = event.currentTarget.dataset.isfold
        var index = this.data.list.findIndex((value) => value.id == id);
        var key ='list[' + index + '].isfold'
        this.setData({
            [key]: !isfold
        })
    },

    searchChange: function (event) {
        var search_content = event.detail.value;
        var str_list = useful_function.getDevidedString(search_content);
        if (search_content == "") {
            this.setData({
                searching: false
            });
            return;
        }
        else {
            this.setData({
                searching: true
            });
            useful_function.matchInit("list", this);
            for (var i = 0; i < str_list.length; i++) {
                useful_function.matchString(str_list[i], "list", "title", this);
                useful_function.matchString(str_list[i], "list", "content", this);
            }
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ user_id: app.globalData.userID })
    },

    getMessage: function() {
        var that = this;
        wx.request({
          url: app.buildUrl('/message/index'),
          header: app.getRequestHeader(),
          data: {
              'user_id': app.globalData.userID
          },
          success: function(res) {
              var resp = res.data;
              if (resp.code == 200) {
                var message_data_list = resp.data.message_data_unread_list.concat(resp.data.message_data_read_list)
                var unread_count = resp.data.message_data_unread_list.length;
                if(unread_count) {
                    wx.setTabBarBadge({
                        index: 0,
                        text: unread_count + "",
                    })
                }
                else {
                    wx.removeTabBarBadge({
                        index: 0,
                    })
                }
                that.setData({
                    list: message_data_list
                })
              }
          }
        })
    },

    goToGroupInfo: function(event) {
        wx.navigateTo({
          url: '/pages/group/info?gid=' + event.currentTarget.dataset.gid,
        })
    },

    goToMessageInfo: function(event) {
        var message_info = this.data.list[parseInt(event.currentTarget.dataset.index)];
        var msg_type = message_info.manager_info.user_id == app.globalData.userID ? 'manager' : 'member';
        app.setCache('current_message_info', message_info);
        wx.navigateTo({
          url: '/pages/message/info?msg_type=' + msg_type,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getMessage();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getMessage();
        setTimeout(function () {
            wx.stopPullDownRefresh();
        }, 2000);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})